package com.example.demo.util;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Base64;

/**
 * @author gsq
 * @date 2025/3/6 17:53
 */
public class ImageUtil {
    public static String generateImage(String base64, String filepath) {
        try {
            base64 = base64.substring(base64.indexOf(",", 1) + 1);
            Base64.Decoder decoder = Base64.getDecoder();
            byte[] imgBytes = decoder.decode(base64);

            for (int i = 0; i < imgBytes.length; ++i) {
                if (imgBytes[i] < 0) {
                    imgBytes[i] += 256;
                }
            }

            OutputStream out = new FileOutputStream(filepath);
            out.write(imgBytes);
            out.flush();
            out.close();
            return filepath;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
